package com.example.book.proxytest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * like aspect
 * 只能代理有接口的类的方法
 */
class JDKProxy implements InvocationHandler {

    // 仍然需要持有真实对象的引用
    private UserInterface userInterface;

    public JDKProxy(UserInterface userInterface) {
        this.userInterface = userInterface;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("Before x");
        Object result = method.invoke(userInterface, args);
        System.out.println("After x");
        return result;
    }
}
